package com.fenqing.leetcode.code.t0_200.t100_200;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

public class Topic200_2 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic200_2(String input) {
        super(input);
    }

    @Execute
    public int numIslands(char[][] grid) {
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }

    public void dfs(char[][] grid, int x, int y){
        int xLen = grid.length, yLen = grid[0].length;
        int xt = x - 1, xb = x + 1, yl = y - 1, yr = y + 1;
        // 上
        if (xt >= 0 && grid[xt][y] == '1') {
            grid[xt][y] = '0';
            dfs(grid, xt, y);
        }
        // 下
        if (xb < xLen && grid[xb][y] == '1') {
            grid[xb][y] = '0';
            dfs(grid, xb, y);
        }
        // 左
        if (yl >= 0 && grid[x][yl] == '1') {
            grid[x][yl] = '0';
            dfs(grid, x, yl);
        }
        // 下
        if (yr < yLen && grid[x][yr] == '1') {
            grid[x][yr] = '0';
            dfs(grid, x, yr);
        }
    }

    public static void main(String[] args) {
        Topic200_2 topic200 = new Topic200_2("""
                [["1"],["1"]]
                """);
        int run = topic200.run();
        System.out.println(run);


    }

}
